//
//  HLNavigationController.swift
//  HLWeiBo
//
//  Created by jack on 15/12/5.
//  Copyright © 2015年 jack. All rights reserved.
//

import UIKit

class HLNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
    }
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //更改左上角的返回按钮
        
        //如果当期导航控制器里面有一个子控制器,那么执行到这个地方的时候,就代表
        //将要push进来第二个,把push进来的控制器的左边按钮改成第一个控制器的title
        //判断内部已经存在过控制器,才执行操作
        if childViewControllers.count != 0 {
        
        var title: String = "返回"
            
            if childViewControllers.count == 1 {
            
                title = childViewControllers.first!.title ?? title
            }
            // 判断如果当前push进来的是rootViewController 就不设置
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName :"navigationbar_back_withtext", title: title, target: self, action: "back")
            
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    @objc private func back(){
        
        popViewControllerAnimated(true)
    }

    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        print("哈哈")
        
        return childViewControllers.count != 1
    }
}



